import axios from 'axios'
import Vue from "vue";
import router from './router/index'
const http = axios.create({
  baseURL:process.env.VUE_APP_API_URL || '/admin/api/'
  //baseURL: 'http://localhost:3000/admin/api/'
})
//全局加请求头
http.interceptors.request.use((config)=>{
  //加请求头
  if (localStorage.token) {
    config.headers.Authorization = 'Bearer '+localStorage.token//||'')
  }
  return config
},(error)=>{
  //错误处理
  return Promise.reject(error)
})
//设置全局状态拦截
http.interceptors.response.use((res)=>{
  return res
},(err)=>{
  //console.log(err.response.data.message);
  if (err.response.data.message) {
    Vue.prototype.$message({
      type:'error',
      message:err.response.data.message
    })//.error(err.response.data.message)

    if (err.response.status === 401) {
      router.push('/login')
    }
  }
  
  return Promise.reject(err)  
})

export default http